package ai.people.core.plugin.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * 热部署工具类
 *
 * @author LiGuanNan
 * @date 2022/6/10 10:56
 */
@Slf4j
public class HotDeployUtils {

    /**
     * 读取jar包中所有类文件
     *
     * @param jarAddress jar地址
     * @return {@link Set}<{@link String}>
     */
    public static Set<String> readJarFile(String jarAddress) {
        Set<String> classNameSet = new HashSet<>();
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(jarAddress);
        } catch (IOException e) {
            log.error("动态加载的jar包路径非法，jarAddress：" + jarAddress);
            e.printStackTrace();
        }
        if (jarFile != null) {
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry jarEntry = entries.nextElement();
                String name = jarEntry.getName();
                if (name.endsWith(".class")) {
                    String className = name.replace(".class", "").replaceAll("/", ".");
                    classNameSet.add(className);
                }
            }
        }
        return classNameSet;
    }

    /**
     * 判断class对象是否带有spring的注解
     *
     * @param cla
     * @return boolean
     */
    public static boolean isSpringBeanClass(Class<?> cla) {
        if (cla == null) {
            return false;
        }
        //是否是接口
        if (cla.isInterface()) {
            return false;
        }
        //是否是抽象类
        if (Modifier.isAbstract(cla.getModifiers())) {
            return false;
        }
        if (cla.getAnnotation(Component.class) != null) {
            return true;
        }
        if (cla.getAnnotation(Repository.class) != null) {
            return true;
        }
        if (cla.getAnnotation(Service.class) != null) {
            return true;
        }
        return cla.getAnnotation(Configuration.class) != null;
    }

    /**
     * 自定义bean名字
     *
     * @param cla 班
     * @return boolean
     */
    public static String getCustomBeanName(Class<?> cla) {
        String beanName = null;
        if (cla == null) {
            return null;
        }
        //是否是接口
        if (cla.isInterface()) {
            return null;
        }
        //是否是抽象类
        if (Modifier.isAbstract(cla.getModifiers())) {
            return null;
        }
        Component component = cla.getAnnotation(Component.class);
        if (component != null) {
            beanName = component.value();
        }
        Repository repository = cla.getAnnotation(Repository.class);
        if (repository != null) {
            beanName = repository.value();
        }
        Service service = cla.getAnnotation(Service.class);
        if (service != null) {
            beanName = service.value();
        }
        RestController controller = cla.getAnnotation(RestController.class);
        if (controller != null) {
            beanName = controller.value();
        }
        Configuration configuration = cla.getAnnotation(Configuration.class);
        if (configuration != null) {
            beanName = configuration.value();
        }
        //默认为类名
        if(StringUtils.isBlank(beanName)){
            beanName = HotDeployUtils.transformName(cla.getName());
        }
        return beanName;
    }

    /**
     * 类名首字母小写 作为spring容器beanMap的key
     */
    public static String transformName(String className) {
        String name = className.substring(className.lastIndexOf(".") + 1);
        return name.substring(0, 1).toLowerCase() + name.substring(1);
    }
}
